package com.mdd.admin.service.impl.yylq;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.service.yylq.ITAppPublicWelfareProjectService;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.validate.yylq.TAppPublicWelfareProjectCreateValidate;
import com.mdd.admin.validate.yylq.TAppPublicWelfareProjectSearchValidate;
import com.mdd.admin.validate.yylq.TAppPublicWelfareProjectUpdateValidate;
import com.mdd.admin.vo.yylq.TAppPublicWelfareProjectDetailVo;
import com.mdd.admin.vo.yylq.TAppPublicWelfareProjectListedVo;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.yangyangLeadingEnterprise.TAppPublicWelfareProject;
import com.mdd.common.mapper.yangyangLeadingEnterprise.TAppPublicWelfareProjectMapper;
import com.mdd.common.util.TimeUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * 领企公益实现类
 * @author LikeAdmin
 */
@Service
public class TAppPublicWelfareProjectServiceImpl implements ITAppPublicWelfareProjectService {
        
    @Resource
    TAppPublicWelfareProjectMapper tAppPublicWelfareProjectMapper;

    /**
     * 领企公益列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<TAppPublicWelfareProjectListedVo>
     */
    @Override
    public PageResult<TAppPublicWelfareProjectListedVo> list(PageValidate pageValidate, TAppPublicWelfareProjectSearchValidate searchValidate) {
        Integer page  = pageValidate.getPage_no();
        Integer limit = pageValidate.getPage_size();

        QueryWrapper<TAppPublicWelfareProject> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");

        tAppPublicWelfareProjectMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "like:name:str",
            "=:subtitle:str",
            "=:pic:str",
            "=:donateAmount@donate_amount:int",
            "=:type:int",
        });

        IPage<TAppPublicWelfareProject> iPage = tAppPublicWelfareProjectMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<TAppPublicWelfareProjectListedVo> list = new LinkedList<>();
        for(TAppPublicWelfareProject item : iPage.getRecords()) {
            TAppPublicWelfareProjectListedVo vo = new TAppPublicWelfareProjectListedVo();
            BeanUtils.copyProperties(item, vo);
            vo.setCreateTime(TimeUtils.timestampToDate(item.getCreateTime().getTime()));
            vo.setUpdateTime(TimeUtils.timestampToDate(item.getUpdateTime().getTime()));
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 领企公益详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return TAppPublicWelfareProject
     */
    @Override
    public TAppPublicWelfareProjectDetailVo detail(Integer id) {
        TAppPublicWelfareProject model = tAppPublicWelfareProjectMapper.selectOne(
                new QueryWrapper<TAppPublicWelfareProject>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        TAppPublicWelfareProjectDetailVo vo = new TAppPublicWelfareProjectDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 领企公益新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Override
    public void add(TAppPublicWelfareProjectCreateValidate createValidate) {
        TAppPublicWelfareProject model = new TAppPublicWelfareProject();
        model.setName(createValidate.getName());
        model.setSubtitle(createValidate.getSubtitle());
        model.setPic(createValidate.getPic());
        model.setCreateTime(new Date());
        model.setUpdateTime(new Date());
        model.setDonateAmount(createValidate.getDonateAmount());
        model.setType(createValidate.getType());
        tAppPublicWelfareProjectMapper.insert(model);
    }

    /**
     * 领企公益编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Override
    public void edit(TAppPublicWelfareProjectUpdateValidate updateValidate) {
        TAppPublicWelfareProject model = tAppPublicWelfareProjectMapper.selectOne(
                new QueryWrapper<TAppPublicWelfareProject>()
                    .eq("id",  updateValidate.getId())
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setName(updateValidate.getName());
        model.setSubtitle(updateValidate.getSubtitle());
        model.setPic(updateValidate.getPic());
        model.setUpdateTime(new Date());
        model.setDonateAmount(updateValidate.getDonateAmount());
        model.setType(updateValidate.getType());
        tAppPublicWelfareProjectMapper.updateById(model);
    }

    /**
     * 领企公益删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        TAppPublicWelfareProject model = tAppPublicWelfareProjectMapper.selectOne(
                new QueryWrapper<TAppPublicWelfareProject>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        tAppPublicWelfareProjectMapper.delete(new QueryWrapper<TAppPublicWelfareProject>().eq("id", id));
    }

}
